"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createCallbackManager = void 0;

var createCallbackManager = function createCallbackManager() {
  var callbacks = [];
  /**
   * 添加回调
   * @param {{ callback: function, ctx: any } | function} opt
   */

  var add = function add(opt) {
    callbacks.push(opt);
  };
  /**
   * 移除回调
   * @param {{ callback: function, ctx: any } | function} opt
   */


  var remove = function remove(opt) {
    var pos = -1;
    callbacks.forEach(function (callback, k) {
      if (callback === opt) {
        pos = k;
      }
    });

    if (pos > -1) {
      callbacks.splice(pos, 1);
    }
  };
  /**
   * 获取回调函数数量
   * @return {number}
   */


  var count = function count() {
    return callbacks.length;
  };
  /**
   * 触发回调
   * @param  {...any} args 回调的调用参数
   */


  var trigger = function trigger() {
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    callbacks.forEach(function (opt) {
      if (typeof opt === 'function') {
        opt.apply(void 0, args);
      } else {
        var callback = opt.callback,
            ctx = opt.ctx;
        callback.call.apply(callback, [ctx].concat(args));
      }
    });
  };

  return {
    add: add,
    remove: remove,
    count: count,
    trigger: trigger
  };
};

exports.createCallbackManager = createCallbackManager;